<template>

  <div style="padding-top: 20px;">
    <el-row>
      <el-col :span="6">
        <el-form-item :label="$tt('label_pms_effective_time')">

          <div style="display: flex; align-items: center; gap: 4px;">
            <el-date-picker
                class="no-clear-input-text"
                v-model="formValue.versionStartTime"
                type="date"
                placeholder="开始日期"
                :clearable="false"
                value-format="timestamp"
                :disabled="versionStartTimeDisabled"
            >
            </el-date-picker>

            <span style="color: #606266;">至</span>
            <el-date-picker
                class="no-clear-input-text"
                v-model="formValue.versionEndTime"
                type="date"
                placeholder="开始日期"
                :clearable="false"
                value-format="timestamp"
            >
            </el-date-picker>
          </div>
        </el-form-item>
      </el-col>
    </el-row>

    <!-- 重量单位 -->
    <el-row>
      <el-col>
        <el-form-item :label="$tt('label_pms_weight_unit')">
          <ex_super_input v-model="formValue.productGlobalConfig.weightUnit" type="select"
                          enum="PmsGlobalWeightUnitEnum"
                          style="width: 100%;"></ex_super_input>
        </el-form-item>
      </el-col>
    </el-row>

    <div style="margin-left: 50px;">
      <el-tabs v-model="activeName"
               :tab-position="tabPosition"
               style="overflow-y: auto"
               type="border-card"
      >
        <el-tab-pane v-for="(item,index) in bizTypeTabs"
                     :label="item.label"
                     :name="item.value"
                     :key="item.value"
        >
          <WarehouseProductBizPrice
              ref="warehouse_product_biz_price" :key="item.value"
              :product-global-config="formValue.productGlobalConfig"
              :bizType="item.value"
              :standardServices="standardServices"
              :additionalServices="additionalServices"
              :priceUnitMap="priceUnitMap"
              :inputForm="matchBizPrice(item.value)"
          />

        </el-tab-pane>
      </el-tabs>
    </div>

    <br>
    <!-- 价卡修改记录  -->
    <el-row style="margin-bottom: 100px;" v-if="formValue.id !== ''">
      <el-col :span="3">
        <el-form-item :label="$tt('label_pms_price_history')">
          <el-timeline style="margin-top: 10px;" class="transprot-price-time-line">
            <el-timeline-item
                v-for="(item, index) in  priceTagHistory"
                :key="index"
                :timestamp="$dateFormat(item.versionPriceTag)"
            >
              {{ item.creator }}
              <ex-button-delete
                  style="position: absolute; left: 220px;"
                  message="确认切换至该版本吗？"
                  label="切换至该版本"
                  byAction
                  :delAction="()=>{ switchPriceVersion(item.id) }"
              ></ex-button-delete>
            </el-timeline-item>
          </el-timeline>

        </el-form-item>
      </el-col>
    </el-row>
  </div>
</template>
<script>

import WarehouseProductBizPrice from '@/component/pms/warehouse_product_biz_price.vue'

export default {
  // title: "新增" + moduleName,
  components: {WarehouseProductBizPrice},
  mixins: [],
  data() {

    return {
      activeName: '',
      tabPosition: 'top',
      bizTypeTabs: [
        {
          value: 'TOC',
          label: 'B2C 价卡配置'
        },
        {
          value: 'TOB',
          label: 'B2B 价卡配置'
        }
      ],
      operateType: "",
      "formValue": {
        "tabId": '',
        "id": '',
        "versionId": '',
        "versionName": '',
        "versionStartTime": '',
        "versionEndTime": '',
        "versionType": '',
        "status": '',
        "versionTag": '',
        "productId": '',
        warehouseGroupPriceVersion: [],
        productGlobalConfig: {
          weightUnit: 'kg'
        }
      },
      // 版本记录
      priceTagHistory: [],

    };
  },

  props: {
    editType: {
      type: Boolean,
      default: true
    },

    versionStartTimeDisabled: {
      type: Boolean,
      default: false
    },
    standardServices: {
      type: Array,
      default: () => []
    },

    additionalServices: {
      type: Array,
      default: () => []
    },

    priceUnitMap: {
      type: Object,
      default: () => {
      }
    },
    priceVersionId: {
      type: String,
      default: '-1'
    },

    inputForm: {
      type: Object,
      default: () => {
      }
    }
  },
  created() {
    this.initForm()
    this.loadPriceVersionTags(this.priceVersionId)
  },
  mounted() {

  },

  watch: {
    // 'inputForm'(val, old) {
    //
    //   )
    // }
  },
  methods: {
    initForm() {
      let inputForm = {}
      inputForm = this.inputForm

      for (let kk in inputForm) {
        this.formValue[kk] = inputForm[kk]
      }

      if (this.formValue.warehouseGroupPriceVersion && this.formValue.warehouseGroupPriceVersion.length > 0) {

        let globalConfig = this.formValue.warehouseGroupPriceVersion[0].productGlobalConfig
        if (_.isEmpty(globalConfig)) {
          globalConfig = {
            weightUnit: 'kg'
          }
        }

        this.$set(this.formValue, 'productGlobalConfig', globalConfig)
      }

      this.activeName = 'TOC'

    },

    matchBizPrice(bizType) {
      let data = {}

      if (!_.isEmpty(this.formValue.warehouseGroupPriceVersion)) {
        let filtered = this.formValue.warehouseGroupPriceVersion.filter(function (value, index, arr) {
          return value.bizType === bizType;
        });
        if (filtered && filtered.length > 0) {
          data = filtered[0];
        } else {
          // 历史数据 默认 全业务一致
          var his = this.formValue.warehouseGroupPriceVersion[0];
          data = JSON.parse(JSON.stringify(his))
        }
      }

      data.bizType = bizType;
      return data
    },

    getWarehouseFormData() {

      let tmp = JSON.parse(JSON.stringify(this.formValue))
      let arr = this.$refs['warehouse_product_biz_price'].map(e => e.getFormData())

      // 价卡数据
      tmp.warehouseGroupPriceVersion = arr

      return tmp
    },

    loadPriceVersionTags(priceVersionId) {
      let _this = this

      // 获取单位相关映射
      _this.$ajax({
        url: '/fms-api/product/PmsWarehouseProductVersionPriceRpc/loadPriceTags.json',
        data: {"priceVersionId": priceVersionId},
        success: (res) => {
          if (res && res.data && res.data.rows) {
            _this.priceTagHistory = res.data.rows
          }
        }
      });
    },

    switchPriceVersion(priceTagId) {
      let _this = this
      // 获取单位相关映射
      _this.$ajax({
        url: '/fms-api/product/PmsWarehouseProductVersionPriceRpc/loadPriceTagDetail.json',
        data: {
          "priceTagId": priceTagId
        },
        success: (res) => {
          if (res && res.data) {
            if (res.data.warehouseGroupPriceVersion && res.data.warehouseGroupPriceVersion.length > 0) {

              let globalConfig = res.data.warehouseGroupPriceVersion[0].productGlobalConfig
              if (_.isEmpty(globalConfig)) {
                globalConfig = {
                  weightUnit: 'kg'
                }
              }

              _this.$set(_this.formValue, 'productGlobalConfig', globalConfig)
              _this.$set(_this.formValue, 'warehouseGroupPriceVersion', res.data.warehouseGroupPriceVersion)

            }
            _this.$message({
              type: 'success',
              message: '切换成功'
            })

          }
        }
      });
    },
  }
};

</script>
<style>

</style>
